package com.ztmg.water.service.base.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import com.ztmg.common.util.resultUtils.JsonResult;
import com.ztmg.water.dto.base.WxImageDTO;
import com.ztmg.water.dto.base.WxImagePageDTO;
import com.ztmg.water.mapper.base.WxImageMapper;
import com.ztmg.water.model.base.WxImage;
import com.ztmg.water.model.base.WxImageExample;
import com.ztmg.water.service.base.WxImageService;
import com.ztmg.water.vo.base.WxImageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WxImageServiceImpl implements WxImageService {

    @Resource
    private WxImageMapper wxImageMapper;
    @Override
    public JsonResult create(WxImageDTO dto) {
        if(null != dto){
            WxImage bean = new WxImage();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreatetime(new Date());
            //后续完善
            bean.setCreater("");
            wxImageMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(WxImageDTO dto) {
        if(null != dto && null != dto.getId()){
            WxImage bean = new WxImage();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifytime(new Date());
            //后续完善
            bean.setModify("");
            wxImageMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<WxImageVO> selectById(Integer id) {
        if(null != id){
            WxImage bean = wxImageMapper.selectByPrimaryKey(id);
            WxImageVO vo = new WxImageVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<WxImageVO>> select(WxImageDTO dto) {
        WxImageExample example = new WxImageExample();
        WxImageExample.Criteria criteria =  example.createCriteria();
        /***
         * 根据具体的业务，查询条件赋值
         */
        List<WxImage> sysUserList = wxImageMapper.selectByExample(example);

        List<WxImageVO> respDTOS = sysUserList.parallelStream().map((bean) -> {
            WxImageVO vo = new WxImageVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<WxImageVO>> selectPage(WxImagePageDTO dto) {
        WxImageExample example = new WxImageExample();
        WxImageExample.Criteria criteria =  example.createCriteria();
        /***
         * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<WxImage> beanList = wxImageMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<WxImageVO> respDTOS = beanList.parallelStream().map((bean) -> {
            WxImageVO vo = new WxImageVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( wxImageMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(WxImageDTO dto) {
        WxImageExample example = new WxImageExample();
        WxImageExample.Criteria criteria =  example.createCriteria();
        /***
         * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(wxImageMapper.deleteByExample(example));
    }
}
